<?php
namespace app\blog\model;
use think\db;
use think\Model;
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
class GoodsModel extends Model
{

    public function __construct($data = [])
    {
        parent::__construct($data);
        $this->id=cmf_get_current_admin_id();
    }
    public  function session()
    {
        $id=cmf_get_current_admin_id();
        $cause['id']=$id;
        $user=DB::name('user')->where($cause)->find();
        if($user['user_type']==1)
        {
            $where='';
            $session=DB::name('shop')->where($where)->select();
        }else{
            $where['re_guid']=$user['shop_guid'];
            $session=DB::name('shop')->where($where)->select()->toArray();
        }
        return $session;
    }
    public function index($cause)
    {
        $session=$this->session();
        if($session)
        {
            if($this->id==1)
            {
                $where='';
            }else{
                $where['go.shop_id']=$session[0]['re_guid'];
            }
            if(isset($cause['g_category']))
            {
                if($cause['g_category'])
                {
                    $where['go.g_category']=$cause['g_category'];
                }
            }
            if(isset($cause['goods_name']))
            {
                if($cause['goods_name']!=null)
                {
                    $where['go.goods_name']=array('like','%'.$cause['goods_name'].'%');
                }
            }
            $data=DB::name('goods')->alias('go')->join(' goodsclass gc ','go.g_category = gc.gc_guid')->join('format f','f.for_guid = go.g_format')->join('brand b','go.brand_id = b.guid')->join('shop s ',' s.re_guid = go.shop_id')->where($where)->field('go.*,gc.gc_name,f.f_name,b.name,s.shop_name')->paginate(10);
            $data->appends($cause);
            $array['data']=$data;
            $array['page']=$data->render();
        }else{
            $array['data']="";
            $array['page']="";
        }

        return $array;
    }
    /*规格查询*/
    public function category($where=null,$state=null)
    {
        $where['status']=1;
        $category=DB::name('goodsclass')->where($where)->select()->toArray();
        $data['gc_guid']=0;
        $data['gc_name']='全部';
        if($state == 1)
        {
            if(!isset($where['gc_guid']))
            {
                foreach ($category as $k=>$v)
                {
                    $category[0]=$data;
                    $category[$k+1]=$v;
                }
            }
        }
        return $category;

    }
    /*系列查询*/
    public function brand()
    {
        $where['status']=1;
        $brand=DB::name('brand')->where($where)->select()->toArray();
        return $brand;
    }
    /*
     * 规格查询*/
    public function format()
    {
        $where['status']=1;
        $brand=DB::name('format')->where($where)->select()->toArray();
        return $brand;
    }
    /*
    * 添加商品
    * */
    public function add($array)
    {
      if(isset($array['shop']))
      {
          $date['goods_guid']=guid();
          $date['goods_name']=$array['goodsname'];
          if(isset($array['pic'])){
              if($array['pic'][0])
              {
                  $date['goods_prc']=$array['pic'][0];
              }else{
                  $date['goods_prc']='';
              }
          }else{
              $date['goods_prc']='';
          }
          $date['g_format']=$array['format'];
          $date['g_category']=$array['category'];
          $date['shop_id']=$array['shop'];
          $date['brand_id']=$array['brandid'];
          $date['g_price']=$array['price'];
          $date['g_newprice']=$array['vipprice'];
          $date['status']=$array['statue'];
          $date['number']=$array['stock'];
          $date['c_time']=time();
          $date['desc']=$array['desc'];
          $date['appointment']=$array['appointment'];
          $state=DB::name('goods')->insert($date);
      }else{
          $state=0;
      }
        return $state;
    }
    /*
    * 更改商品
    * */
    public function detail($date)
    {
        $where['goods_guid']=$date['goods_guid'];
        $date['goods_prc']=substr($date['goods_prc'],strrpos($date['goods_prc'],'blog'));
        $date['u_time']=time();
        $state=DB::name('goods')->where($where)->update($date);
        return $state;

    }

    /*
    * 删除类别
    * 查询是否有该订单，有订单的不让删除
    * */
    public function del($date)
    {
       $where['goods_id']=$date['goods_guid'];
       $order_detail=DB::name('order_detail')->where($where)->find();
       if($order_detail)
       {
           $state['num']=0;
           $state['con']='订单里存在该商品，建议下架!';
       }else{
           $cause['goods_guid']=$date['goods_guid'];
           $goods=DB::name('goods')->where($cause)->delete();
           if($goods)
           {
               $state['num']=$goods;
               $state['con']='删除成功';
           }else{
               $state['num']=$goods;
               $state['con']='删除失败';
           }
       }
       return $state;

    }
    /*获取单品商品*/
    public function  getone($where)
    {
        $data=DB::name('goods')->where($where)->find();
        return $data;
    }


}